<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <meta charset="UTF-8" />
        <title>09_案例练习-导航栏跟随</title>
        <style>
            * {
                margin: 0;
                padding: 0;
            }
            div {
                position: absolute;
                width: 1366px;
                height: 160px;
                background-color: #ccc;
                text-align: center;
                line-height: 160px;

            }

            input {
                width: 700px;
                height: 40px;
                border: 1px solid orangered;
                border-radius: 20px;
                outline: none;
            }
        </style>
    </head>
    <body style="height: 3000px;">
        <div>
            <input type="text">
        </div>

        <script>
            // 如果滚动条滚动了 一个视口的高度 那么就将div固定在最上面

            var div = document.querySelector('div');


            document.onscroll = function(){
                // 滚动的距离
                var scroll_dis = document.documentElement.scrollTop;

                // 视口的高度
                // var vp_height = document.documentElement.clientHeight;

                if(scroll_dis >= 100){
                    div.style.position = 'fixed';
                }else {
                    div.style.position = 'static';
                }

            }

        </script>
    </body>
</html>